package common.ui.controls
{
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	

	[Event(name="close", type="flash.events.Event")]
	public class SimpleDragWindow extends Sprite
	{
		public static const BEFORE_ADD_TO_STAGE_INIT:String = "beforeAddToStageInit";
		public var isAlphaShow:Boolean = true;
		public var isSetXY:Boolean = false;
		
		protected var _closeButton:DisplayObject;
		protected var _dragArea:Shape;
		protected var _dragButton:SimpleButton;
		protected var _dragRect:Rectangle;
		protected var _lastStageWidth:Number = 0;
		protected var _lastStageHeight:Number = 0;
		protected var _alphas:Array;
		protected var _alphaIndex:int;
		protected var _lockWidth:int = -1;
		protected var _lockHeight:int = -1;
		protected var _lockX:int = 0;
		protected var _lockY:int = 0;
		protected var _isHasSetDragArea:Boolean;
		protected var _isAliagned:Boolean=false;
		protected var _dragAlpha:Number = 0.6;
		protected var _dis:Sprite;
		
		public function SimpleDragWindow()
		{
			_alphas = [0.8,0.9,1.0];
			_dragArea = new Shape();
			_dragButton = new SimpleButton();
			_dragButton.useHandCursor = false;
			_dragButton.addEventListener(MouseEvent.MOUSE_DOWN,startDragMe);
			this.addEventListener(Event.ADDED_TO_STAGE,onAddToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveStage);
			this.addEventListener(MouseEvent.MOUSE_DOWN,setToTop);
		}
		
		public function get dragAlpha():Number
		{
			return _dragAlpha;
		}

		public function set dragAlpha(value:Number):void
		{
			_dragAlpha = value;
		}

		public function close(event:MouseEvent=null):void
		{
			if(this.parent != null)
			{
				this.stage.removeEventListener(Event.RESIZE,checkMove);
				this.stage.removeEventListener(MouseEvent.MOUSE_UP,stopDragMe);
				this.stage.removeEventListener(MouseEvent.MOUSE_MOVE,updateDrag);
				this.parent.removeChild(this);
				this.dispatchEvent(new Event("close"));
			}
		}
		protected function setDragArea(areaWidth:Number,areaHeight:Number=27,startX:Number=0,startY:Number=0):void
		{
			_isHasSetDragArea = true;
			_dragArea.graphics.clear();
			_dragArea.graphics.beginFill(0);
			_dragArea.graphics.drawRect(startX,startY,areaWidth,areaHeight);
			_dragButton.hitTestState = _dragArea;
			this.addChild(_dragButton);
		}
		public function setCloseButton(closeButton:DisplayObject=null):void
		{
			if(closeButton == null)
			{
				closeButton = _dis.getChildByName("close_btn");
			}
			closeButton.addEventListener(MouseEvent.CLICK,close);
			_closeButton = closeButton;
		}
		public function alignTo(location:String="center"):void
		{
			if(_lockWidth == -1 && _lockHeight == -1){
				this.x = (this.stage.stageWidth-this.width)/2;
				this.y = (this.stage.stageHeight - this.height)/2;
			}else
			{
				this.x = (_lockWidth-this.width)/2;
				this.y = (_lockHeight - this.height)/2;
			}
			this.x = int(this.x);
			this.y = int(this.y);
			if(this.x < 0) this.x = 0;
			if(this.y < 0) this.y = 0;
		}
		protected function onAddToStage(event:Event):void
		{
			this.dispatchEvent(new Event(BEFORE_ADD_TO_STAGE_INIT));
			if(_isHasSetDragArea == false)
			{
				setDragArea(this.width-35);
			}
			if(isSetXY == false)
			{
				if(_isAliagned == false)
				{
					alignTo();
					_isAliagned = true;
				}
			}
			this.stage.addEventListener(Event.RESIZE,checkMove);
			checkMove();
			_alphaIndex = 0;
			if(isAlphaShow == true){
				this.addEventListener(Event.ENTER_FRAME,alphaShowFrame);
				alphaShowFrame();
			}
		}
		protected function onRemoveStage(event:Event):void
		{
			this.stage.removeEventListener(Event.RESIZE,checkMove);
		}
		protected function alphaShowFrame(event:Event=null):void
		{
			this.alpha = _alphas[_alphaIndex];
			_alphaIndex++;
			if(_alphaIndex>=_alphas.length)
			{
				this.removeEventListener(Event.ENTER_FRAME,alphaShowFrame);
			}
		}
		protected function checkMove(event:Event=null):void
		{
			if(_lastStageWidth != 0 && _lastStageHeight != 0){
				this.x = int(this.x*this.stage.stageWidth/_lastStageWidth);
				this.y = int(this.y*this.stage.stageHeight/_lastStageHeight);
			}
			if(_lockWidth == -1 && _lockHeight == -1){
				
				_dragRect = new Rectangle(_lockX,_lockY,this.stage.stageWidth-this.width,this.stage.stageHeight-this.height);
			}else
			{
				_dragRect = new Rectangle(_lockX,_lockY,_lockWidth-this.width,_lockHeight-this.height);
			}
			if( _dragRect.width <= 0 )
			{
				_dragRect.width = this.stage.stageWidth - _dis.width;
			}
			if( _dragRect.height <= 0 ) 
			{
				_dragRect.height = this.stage.stageHeight - _dis.height;
			}
			if(this.x > _dragRect.width) this.x = _dragRect.width;
			if(this.y > _dragRect.height) this.y = _dragRect.height;
			if(this.x < 0 ) this.x = 0;
			if(this.y < 0 ) this.y = 0;
			_lastStageWidth = this.stage.stageWidth;
			_lastStageHeight = this.stage.stageHeight;
		}
		protected function startDragMe(event:MouseEvent):void
		{
			if(_lockWidth == -1 && _lockHeight == -1){
				_dragRect = new Rectangle(_lockX,_lockY,this.stage.stageWidth-this.width,this.stage.stageHeight-this.height);
			}else
			{
				_dragRect = new Rectangle(_lockX,_lockY,_lockWidth-this.width,_lockHeight-this.height);
			}
			if( _dragRect.width <= 0 )
			{
				_dragRect.width = this.stage.stageWidth - _dis.width;
			}
			if( _dragRect.height <= 0 ) 
			{
				_dragRect.height = this.stage.stageHeight - _dis.height;
			}
			this.startDrag(false,_dragRect);
			this.cacheAsBitmap = true;
			this.alpha = _dragAlpha;
			this.cacheAsBitmap = true;
			this.stage.addEventListener(MouseEvent.MOUSE_UP,stopDragMe);
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE,updateDrag);
		}
		
		protected function updateDrag(event:MouseEvent):void
		{
			event.updateAfterEvent();
		}
		protected function stopDragMe(event:MouseEvent):void
		{
			this.stopDrag();
			this.cacheAsBitmap = false;
			this.alpha = 1;
			this.cacheAsBitmap = false;
			if(this.stage != null)
			{
				this.stage.removeEventListener(MouseEvent.MOUSE_UP,stopDragMe);
				this.stage.removeEventListener(MouseEvent.MOUSE_MOVE,updateDrag);
			}
		}
		protected function setToTop(event:MouseEvent):void
		{
			this.parent.setChildIndex(this,this.parent.numChildren-1);
		}		
	}
}